<?php defined('BASEPATH') OR exit('No direct script access allowed');

	class trimesterguide_class extends CI_Model {
	
		public function list_trimesterguide($post){
			if(isset($post['intCode'])){
				$intCode = stripslashes(filter_var($post['intCode'], FILTER_SANITIZE_STRING));
				$this->db->where(array('intCode'=>$intCode));
			}
			$this->db->order_by("intOrder", "asc");
			$query = $this->db->get_where('jtn_trimesterguide',array('intDisabled'=>0));
			
			if ($query->num_rows() > 0){
				$result = $query->result_array();
				return $result;
			}
			else{
				return 0;
			}
		}
		
		// Additional function to get data using intCode
		public function get_trimesterguide($post){
			$intCode = 0;
			if(isset($post['intCode'])){
				$intCode = $post['intCode'];
			}
			$intCode = stripslashes(filter_var($intCode, FILTER_SANITIZE_STRING));
			$query = $this->db->get_where('jtn_trimesterguide',array('intCode'=>$intCode))->result_array();
			$query = current($query);
				
			if(trim($query['intCode'])=="") {
				return 0;
			} else {
				return $query;
			}
		}		

		public function update($post){
			if(isset($post['intCode'])){
				$intCode = stripslashes(filter_var($post['intCode'], FILTER_SANITIZE_STRING));
				$strTitle = stripslashes(filter_var($post['strTitle'], FILTER_SANITIZE_STRING));
				$strNote = stripslashes(filter_var($post['strNote'], FILTER_SANITIZE_STRING));
				$strImgLink = stripslashes(filter_var($post['strImgLink'], FILTER_SANITIZE_STRING));
		
				$data = array(
						'intCode' => $intCode,
						'strTitle' => $strTitle,
						'strNote' => $strNote,
						'strImgLink' => $strImgLink,
						'strModifiedBy'=>$this->session->userdata('intID'),
						'dateModifiedDate'=>date('Y-m-d H:i:s')
				);
			} else {
				$strTitle = stripslashes(filter_var($post['strTitle'], FILTER_SANITIZE_STRING));
				$strNote = stripslashes(filter_var($post['strNote'], FILTER_SANITIZE_STRING));
				$strImgLink = stripslashes(filter_var($post['strImgLink'], FILTER_SANITIZE_STRING));
		
				$data = array(
						'strTitle' => $strTitle,
						'strNote' => $strNote,
						'strImgLink' => $strImgLink,
						'strModifiedBy'=>$this->session->userdata('intID'),
						'dateModifiedDate'=>date('Y-m-d H:i:s')
				);
			}
		
			$this->db->where(array('intCode'=>$intCode));
			$this->db->update('jtn_trimesterguide', $data);
		
			if($this->db->_error_number()==0)
			{
				$message['fail'] = 0;
				$message['class'] = "success";
				$message['message'] = "Successfully saved.";
				$message['intID'] = $this->db->insert_id();
			}
			else
			{
				$message['fail'] = 1;
				$message['class'] = "error";
				$message['message'] = $message['message'].$this->db->_error_message();;
			}
			return $message;
		}
		
		public function insert($post){
			if(isset($post['intCode'])){
				$intCode = stripslashes(filter_var($post['intCode'], FILTER_SANITIZE_STRING));
				$strTitle = stripslashes(filter_var($post['strNoteTitle'], FILTER_SANITIZE_STRING));
				$strNote = stripslashes(filter_var($post['strNotes'], FILTER_SANITIZE_STRING));
				$strImgLink = stripslashes(filter_var($post['addTrimesterguidestrImgLink'], FILTER_SANITIZE_STRING));
		
				$data = array(
						'intCode' => $intCode,
						'strTitle' => $strTitle,
						'strNote' => $strNote,
						'strImgLink' => $strImgLink,
						'intDisabled' => 0,
						'strCreatedby'=>$this->session->userdata('intID'),
						'dateCreatedDate'=>date('Y-m-d H:i:s'),
						'strModifiedBy'=>$this->session->userdata('intID'),
						'dateModifiedDate'=>date('Y-m-d H:i:s')
				);
			} else {
				$strTitle = stripslashes(filter_var($post['strNoteTitle'], FILTER_SANITIZE_STRING));
				$strNote = stripslashes(filter_var($post['strNotes'], FILTER_SANITIZE_STRING));
				$strImgLink = stripslashes(filter_var($post['strImgLink'], FILTER_SANITIZE_STRING));
		
				$data = array(
						'strTitle' => $strTitle,
						'strNote' => $strNote,
						'strImgLink' => $strImgLink,
						'intDisabled' => 0,
						'strCreatedby'=>$this->session->userdata('intID'),
						'dateCreatedDate'=>date('Y-m-d H:i:s'),
						'strModifiedBy'=>$this->session->userdata('intID'),
						'dateModifiedDate'=>date('Y-m-d H:i:s')
				);
			}
		
			$this->db->insert('jtn_trimesterguide', $data);
				
			$last_id = $this->db->insert_id();
			$this->db->where(array('intID'=>$last_id));
			$this->db->update('jtn_trimesterguide', array('intCode'=>$last_id, 'intOrder'=>$last_id));
			//$message['qry']=$this->db->last_query();
			if($this->db->_error_number()==0)
			{
				$message['fail'] = 0;
				$message['class'] = "success";
				$message['message'] = "Successfully saved.";
				$message['intID'] = $this->db->insert_id();
			}
			else
			{
				$message['fail'] = 1;
				$message['class'] = "error";
				$message['message'] = $message['message'].$this->db->_error_message();;
			}
			return $message;
		}
		public function delete($post){
			$intCode = stripslashes(filter_var($post['intCode'], FILTER_SANITIZE_STRING));
		
			$data = array(
					'intDisabled' => 1,
					'strModifiedBy'=>$this->session->userdata('intID'),
					'dateModifiedDate'=>date('Y-m-d H:i:s')
			);
		
			$this->db->where(array('intCode'=>$intCode));
			$this->db->update('jtn_trimesterguide', $data);
		
			if($this->db->_error_number()==0)
			{
				$message['fail'] = 0;
				$message['class'] = "success";
				$message['message'] = "Successfully saved.";
				$message['intID'] = $this->db->insert_id();
			}
			else
			{
				$message['fail'] = 1;
				$message['class'] = "error";
				$message['message'] = $message['message'].$this->db->_error_message();;
			}
			return $message;
		}		
	} // end class
?>